package com.app.twth.ui.b2cverify.submit;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;

import com.app.twth.BR;
import com.app.twth.R;
import com.app.twth.app.AppRouter;
import com.app.twth.base.BaseActivity;
import com.app.twth.databinding.ActivityB2cVerifySubmitBinding;
import com.app.twth.databinding.DialogSubmitSucceedBinding;
import com.app.twth.ui.b2cverify.B2CVerify;
import com.app.twth.ui.b2cverify.B2CVerifyViewModel;
import com.app.twth.utils.MyDialogUtil;
import com.app.twth.utils.ToastUtil;
import com.app.twth.utils.Utils;

import java.util.Timer;
import java.util.TimerTask;

/**
 * @describe :
 */
public class B2CVerifySubmitActivity extends BaseActivity<ActivityB2cVerifySubmitBinding, B2CVerifyViewModel> {
    private String TAG = getClass().getSimpleName();
    String trk;
    @Override
    protected void initListener() {
        viewModel.isCheck.observe(this, data -> {
            showSubmitSucceedDialog();
        });
        viewModel.errorResultCode.observe(this, code -> {
            showHint((String) code);
        });
        viewModel.isLoading.observe(this, b -> {
            if (b.isShow()) {
                showProgressDialog(b.getMsg());
            } else {
                dismissProgressDialog();
            }
        });
    }

    protected String setTitle() {
        return "B2C:OBVerify";
    }

    @Override
    protected int initViewModelId() {
        return BR.viewModel;
    }

    @Override
    protected void initData() {
        setNext("Submit");
        Bundle bundle = getIntent().getBundleExtra(AppRouter.K_EXTRA);
        trk = bundle.getString("data");
        binding.tvTrk.setText(Utils.getStr(trk));
        binding.tvCode.requestFocus();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        bottomLayoutBinding.tvHint.setText("");
        initScan();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        unScan();
    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_scan:
                setScan();
                break;
            case R.id.tv_reset:
                binding.tvCode.setText("");
                break;
            case R.id.tv_next:
                if (System.currentTimeMillis() - timeMillis < spaceTime) {//两秒内触发一次
                    return;
                }
                timeMillis = System.currentTimeMillis();
                barcodeStr =  binding.tvCode.getText().toString();
                if (Utils.isEmpty(barcodeStr)) {
                    ToastUtil.showToast("Please enter the trailer");
                    return;
                }
                B2CVerify b2CVerify = new B2CVerify();
                b2CVerify.setTrackingCode(trk);
                b2CVerify.setSku(barcodeStr);
                viewModel.b2cVerifySku(b2CVerify);
                break;
        }
    }

    private AlertDialog dialogSubmitSucceed;

    private void showSubmitSucceedDialog() {
        dialogSubmitSucceed = MyDialogUtil.getDialog(this, initSubmitSucceedDialogView(), Gravity.CENTER);
        dialogSubmitSucceed.setCanceledOnTouchOutside(false);
        dialogSubmitSucceed.show();
    }

    private View initSubmitSucceedDialogView() {
        DialogSubmitSucceedBinding dialogBinding = DialogSubmitSucceedBinding.inflate(getLayoutInflater());
        dialogBinding.tvCancel.setOnClickListener(v -> {
            if (dialogSubmitSucceed != null) {
                dialogSubmitSucceed.dismiss();
            }
            Intent intent = new Intent();
            setResult(RESULT_OK, intent);
            finish();
        });
        dialogBinding.tvConfirm.setVisibility(View.GONE);
        startTime();
        return dialogBinding.getRoot();
    }


    private Timer timer;
    private void startTime() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();
            }
        }, 2 * 1000, 2 * 1000);
    }

    private void endTime() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        if (dialogSubmitSucceed != null) {
            dialogSubmitSucceed.dismiss();
        }
    }
    @Override
    public void getScanninCode() {
        super.getScanninCode();
        binding.tvCode.setText(barcodeStr);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        endTime();
    }
}
